<template>
  <div class="container">
    <!-- 头部 -->
    <el-breadcrumb
      v-if="subjects.id && subject.name"
      separator-class="el-icon-arrow-right"
    >
      <el-breadcrumb-item>学科管理</el-breadcrumb-item>
      <el-breadcrumb-item>{{
        subjects.id ? subject.name : ''
      }}</el-breadcrumb-item>
      <el-breadcrumb-item>项目管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <!-- 搜索栏 -->
    <el-row>
      <el-col :span="16">
        <el-row>
          <el-col :span="16">
            <el-form :model="Taglist" label-width="70px">
              <el-form-item label="标签名称" style="display: inline-block">
                <el-input
                  class="inputCol"
                  v-model="Taglist.directoryName"
                  style="width: 200px"
                ></el-input>
              </el-form-item>
              <el-form-item style="display: inline-block" label="状态">
                <el-select v-model="Taglist.state" placeholder="请选择">
                  <el-option label="已启用" :value="0"> </el-option>
                  <el-option label="已禁用" :value="1"> </el-option>
                </el-select>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="8">
            <el-button class="colInput" plain @click="removeVal"
              >清除</el-button
            >
            <el-button type="primary" @click="gettags">搜索</el-button>
          </el-col>
        </el-row>
      </el-col>
      <el-col :span="8" style="display: inline-block; width: 400px">
        <el-button
          style="float: right"
          type="text"
          @click="$router.back()"
          v-if="subjects.id && subjects.name"
          class="el-icon-back"
          >返回学科</el-button
        >
        <el-button
          style="float: right"
          @click="dialogVisible = true"
          icon="el-icon-edit"
          type="success"
          >新增标签</el-button
        >
        <!-- 添加弹窗 -->
        <el-dialog title="新增标签" :visible.sync="dialogVisible" width="30%">
          <!-- 添加模块 -->
          <el-row>
            <el-col>
              <el-form
                :model="tagForm"
                :rules="tagFromRules"
                label-width="80px"
                class="demo-ruleForm"
              >
                <el-form-item
                  v-if="!subjects.id && !subjects.name"
                  label="所属学科"
                >
                  <el-select
                    v-model="tagForm.value"
                    style="width: 100%"
                    placeholder="请选择"
                  >
                    <el-option
                      :label="i.label"
                      :value="i.value"
                      v-for="i in subjectsList"
                      :key="i.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item class="elform" label="标签名称" prop="tagName">
                  <el-input
                    v-model="tagForm.tagName"
                    placeholder="请输入标签名称"
                    style="width: 100%"
                  ></el-input>
                </el-form-item>
              </el-form>
            </el-col>
          </el-row>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addFn">确 定</el-button>
          </span>
        </el-dialog>
      </el-col>
    </el-row>
    <!-- 显示数据 -->
    <el-alert
      v-if="this.counts !== ''"
      :title="`共${this.counts}条记录`"
      class="el-icon-info shuju"
      :closable="false"
    ></el-alert>
    <!-- 标签数据 -->
    <el-table :data="dataList" style="width: 100%">
      <el-table-column type="index" label="序号"> </el-table-column>
      <el-table-column prop="subjectName" label="所属学科"> </el-table-column>
      <el-table-column prop="tagName" label="标签名称"> </el-table-column>
      <el-table-column prop="username" label="创建者"> </el-table-column>
      <el-table-column prop="addDate" label="创建日期"> </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          {{ scope.row.state === 1 ? '已禁用' : '已启用' }}
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="changeState(scope.row)">{{
            scope.row.state === 1 ? '启用' : '禁用'
          }}</el-button>
          <el-button
            @click="editTagFn(scope.row)"
            type="text"
            :disabled="scope.row.state === 1"
            >修改</el-button
          >
          <el-button
            @click="removeFn(scope.row)"
            type="text"
            :disabled="scope.row.state === 1"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 修改弹窗 -->
    <el-dialog title="修改标签" :visible.sync="editdialogVisible" width="30%">
      <!-- 修改模块 -->
      <el-row>
        <el-col>
          <el-form
            :model="editTagForm"
            :rules="tagFromRules"
            label-width="80px"
            class="demo-ruleForm"
          >
            <el-form-item
              v-if="!subjects.id && !subjects.name"
              label="所属学科"
            >
              <el-select
                v-model="editTagForm.value"
                style="width: 100%"
                placeholder="请选择"
              >
                <el-option
                  :label="i.label"
                  :value="i.value"
                  v-for="i in subjectsList"
                  :key="i.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item class="elform" label="标签名称" prop="tagName">
              <el-input
                v-model="editTagForm.tagName"
                placeholder="请输入标签名称"
                style="width: 100%"
              ></el-input>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editFn">确 定</el-button>
      </span>
    </el-dialog>
    <!--  -->
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="Taglist.page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="Taglist.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="counts"
    >
    </el-pagination>
  </div>
</template>

<script>
import {
  list,
  changeState,
  add,
  update,
  remove,
  simples
} from '@/api/hmmm/tags.js'
export default {
  data() {
    return {
      dataList: [], // 标签数据列表
      subjectsList: [], // 学科列表
      // 请求参数
      Taglist: {
        page: 1,
        pagesize: 10,
        directoryName: '',
        state: null
      },
      dialogVisible: false, // 添加弹出层默认关闭
      editdialogVisible: false, // 修改弹出层默认关闭
      // 添加窗口的数据
      tagForm: {
        tagName: '',
        value: ''
      },
      // 标签id
      tageId: '',
      // 修改窗口的数据
      editTagForm: {
        tagName: '',
        value: ''
      },
      tagFromRules: {
        tagName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ]
      },
      counts: 0 // 标签总数
    }
  },
  computed: {
    subjects() {
      return this.$route.query || null
    }
  },
  watch: {
    '$router.query'() {
      this.gettags()
    }
  },
  created() {
    this.gettags()
    this.getsubjectsList()
  },
  methods: {
    // 清除
    removeVal() {
      this.Taglist.directoryName = ''
      this.Taglist.state = null
      this.gettags()
    },
    // 删除数据
    async removeFn(row) {
      const configRuselt = await this.$confirm(
        '此操作将永久删除该学科, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)
      if (configRuselt !== 'confirm') {
        return this.$message('您取消了删除操作!')
      }
      try {
        await remove(row)
        this.$message.success('删除成功!')
        this.gettags()
      } catch (e) {
        this.$message.error('删除失败!')
      }
    },
    // 点击确定
    async editFn() {
      try {
        if (this.subjects.id) {
          await update({
            id: this.tageId,
            subjectID: +this.subjects.id,
            tagName: this.editTagForm.tagName
          })
          this.$message.success('修改成功')
          this.editdialogVisible = false
          this.gettags()
        } else {
          await update({
            id: this.tageId,
            subjectID: this.editTagForm.value,
            tagName: this.editTagForm.tagName
          })
          this.$message.success('修改成功')
          this.editdialogVisible = false
          this.gettags()
        }
      } catch (e) {
        this.$success.error('修改失败')
      }
    },

    // 点击修改
    editTagFn(row) {
      this.editTagForm.value = row.subjectID
      this.tageId = row.id
      this.editTagForm.tagName = row.tagName
      this.editdialogVisible = true
    },
    // 添加数据
    async addFn() {
      try {
        if (this.subjects.id) {
          await add({
            subjectID: this.subjects.id,
            tagName: this.tagForm.tagName
          })
          this.$message.success('添加成功')
          this.dialogVisible = false
          this.gettags()
        } else {
          await add({
            subjectID: this.tagForm.value,
            tagName: this.tagForm.tagName
          })
          this.$message.success('添加成功')
          this.dialogVisible = false
          this.gettags()
        }
        this.tagForm.value = ''
        this.tagForm.tagName = ''
      } catch (e) {
        this.$message.error('添加失败')
      }
    },
    // 点击改变数据状态
    async changeState(row) {
      try {
        await changeState({
          id: row.id,
          state: row.state === 1 ? 0 : 1
        })
        row.state = row.state === 1 ? 0 : 1
        this.$message.success('操作成功')
        this.gettags()
      } catch (e) {
        this.$message.error('操作失败')
      }
    },
    // 获取学科列表数据
    async getsubjectsList() {
      try {
        const { data } = await simples()
        this.subjectsList = data
      } catch (e) {
        this.$message.error('获取失败')
      }
    },
    // 每页几条数据
    handleSizeChange(newsize) {
      this.Taglist.pagesize = newsize
      this.gettags()
    },
    // 当前第几页
    handleCurrentChange(newPage) {
      this.Taglist.page = newPage
      this.gettags()
    },
    // 获取标签列表数据
    async gettags() {
      try {
        this.Taglist.subjectID = this.subjects.id || null
        const { data } = await list(this.Taglist)
        this.dataList = data.items
        this.counts = data.counts
      } catch (e) {
        this.$message.error('请求失败')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  padding: 20px;
  .el-pagination {
    margin: 10px 0;
  }
}
</style>
